#include <functional>
#include <iostream>
#include <queue>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

int main() {
    string s;
    while (cin >> s) {
        unordered_map<char, int> m;
        for (auto it : s)
            m[it]++;

        priority_queue<int, vector<int>, greater<int>> pq;
        for (auto it : m)
            pq.push(it.second);
        if (pq.size() == 1) {
            cout << s.size() << endl;
            continue;
        }

        int res = 0;
        while (pq.size() > 1) {
            int a = pq.top();
            pq.pop();
            int b = pq.top();
            pq.pop();
            int merge = a + b;
            res += merge;
            pq.push(merge);
        }
        cout << res << endl;
    }
    return 0;
}